/*
 * Copyright 2009 Kaspar Minosiants http://www.minosiants.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.minosiants.confluence.blog.plugin.impl.tweet2it;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.atlassian.spring.container.ContainerManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.minosiants.confluence.blog.plugin.tweet2it.IHttpClient;
import com.minosiants.confluence.blog.plugin.tweet2it.ITwitterClient;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterDirectMessage;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterStatus;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterUser;

/**
 *	Project home: http://tweet2it.minosiants.com
 *
 *  @author Kaspar Minosiants
 *
 * 	$Id:$
 */
public class TwitterClient implements ITwitterClient {
	
	private Log logger = LogFactory.getLog(getClass());
	private static String TWITTER_HOST="https://twitter.com";	
	private IHttpClient httpClient; 
	private Gson gson;		
	
	public TwitterClient() {		
		gson=createGson();
	}

	private Gson createGson() {
	    GsonBuilder gsonBuilder=new GsonBuilder();
	    Locale.setDefault(Locale.US);
	    gsonBuilder.setDateFormat("EEE MMM dd kk:mm:ss Z yyyy");
	    return gsonBuilder.create();	
	}

	
	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getFriendsTimeLine()
	 */
	public Collection<TwitterStatus> getFriendsTimeLine(Long page) {
		String response=getHttpClient().get(TWITTER_HOST+"/statuses/friends_timeline.json"+appendPage(page));
	    Type collectionType = new TypeToken<Collection<TwitterStatus>>(){}.getType();
	   return gson.fromJson(response, collectionType);	   
	}


	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getUserTimeline()
	 */
	public Collection<TwitterStatus> getUserTimeline(Long page){
		String response=getHttpClient().get(TWITTER_HOST+"/statuses/user_timeline.json"+appendPage(page));
		Type collectionType = new TypeToken<Collection<TwitterStatus>>(){}.getType();
		return gson.fromJson(response, collectionType);
	}	
	

	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getPublicTimeline()
	 */
	public Collection<TwitterStatus> getPublicTimeline(Long page){
		String response=getHttpClient().get(TWITTER_HOST+"/statuses/public_timeline.json"+appendPage(page));
		Type collectionType = new TypeToken<Collection<TwitterStatus>>(){}.getType();
		return gson.fromJson(response, collectionType);
	}			
	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getFavorites()
	 */
	public Collection<TwitterStatus> getFavorites(Long page){
		String response=getHttpClient().get(TWITTER_HOST+"/favorites.json"+appendPage(page));
		Type collectionType = new TypeToken<Collection<TwitterStatus>>(){}.getType();
		return gson.fromJson(response, collectionType);
	}
	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getMentions()
	 */
	public Collection<TwitterStatus> getMentions(Long page){
		String response=getHttpClient().get(TWITTER_HOST+"/statuses/mentions.json"+appendPage(page));
		Type collectionType = new TypeToken<Collection<TwitterStatus>>(){}.getType();
		return gson.fromJson(response, collectionType);
	}	
	/* (non-Javadoc)
	 * @see com.minosiants.confluence.blog.plugin.twitter.ITwitterClient#getTwitterUser(java.lang.String)
	 */
	public TwitterUser getTwitterUser(String screanName){	
		String response=getHttpClient().get(TWITTER_HOST+"/users/show/"+screanName+".json");		
		return gson.fromJson(response,TwitterUser.class);
	}
	@Override
	public Collection<TwitterDirectMessage> getDirect(Long page) {
		String response=getHttpClient().get(TWITTER_HOST+"/direct_messages.json"+appendPage(page));
		Type collectionType = new TypeToken<Collection<TwitterDirectMessage>>(){}.getType();
		return gson.fromJson(response, collectionType);
	}
	public boolean addToFavorites(Long id) {
		getHttpClient().post(TWITTER_HOST+"/favorites/create/"+id+".json");
		return true;
	}

	public boolean removeFromFavorites(Long id) {
		getHttpClient().post(TWITTER_HOST+"/favorites/destroy/"+id+".json");
		return true;
	}

	public TwitterStatus updateStatus(String status){
		return updateStatus(status,null);
	}
	public TwitterStatus updateStatus(String status,String inReplyToStatusId){		
		Map<String, String> params=new HashMap<String, String>();
		params.put("status", status);
		if(StringUtils.isNotBlank(inReplyToStatusId))
			params.put("in_reply_to_status_id", inReplyToStatusId.toString());
		return gson.fromJson(httpClient.post(TWITTER_HOST+"/statuses/update.json",params),TwitterStatus.class);
	}
	@Override
	public boolean deleteDirectMessage(String id) {
		httpClient.post(TWITTER_HOST+"/direct_messages/destroy/"+id+".json");
		return true;
	}
	private String appendPage(Long page) {		
		return page==null ?"":"?page="+page;
	}
    public boolean deleteStatus(Long statusId) {
    	httpClient.post(TWITTER_HOST+"/statuses/destroy/"+statusId+".json");
    	return true;
    }

	public void setHttpClient(IHttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public IHttpClient getHttpClient() {
		if(httpClient==null)
			ContainerManager.autowireComponent(this);
		return httpClient;

	}


    

}
